package com.ms.service.module.datacenterlite;

import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import com.ms.game.slot.base.ISlotDoormanProcesser;
import com.ms.service.error.ErrorCode;
import com.ms.service.module.datacenterlite.constant.DCLiteMessageType;
import com.ms.service.module.zone.BasicZone;
import com.xpec.c4.db.dao.PlayerKeepSeatDAO;
import com.xpec.c4.db.dao.PlayerKeepSeatLogDAO;
import com.xpec.c4.db.model.PlayerKeepSeatLogModel;
import com.xpec.c4.db.model.PlayerKeepSeatModel;
import com.xpec.c4.game.time.TimeSys;
import com.xpec.c4.service.contexts.Contexts;

public class GameCancelKeepDataAyncCommand extends CentralAsyncCommand {
	/**
	 * 消息對應method type;
	 */
	public int methodType;

	public boolean isofflinekeep;

	public String gameName;

	public String halltype;

	public String seatid;

	public String playerGUID;

	public int machineid;

	public int errorCode;

	/**
	 * 
	 */
	private static final long serialVersionUID = -420240935330718878L;

	@SuppressWarnings("unchecked")
	@Override
	public void asyncExecute() throws Exception {
		ConcurrentHashMap<String, GameKeepSeatAllData> keep = DataCenterLiteModule.getKeepSeat();

		String guid = playerGUID;

		if (!keep.containsKey(guid)) {
			errorCode = ErrorCode.DATA_NOT_FOUND;
			return;
		}

		GameKeepSeatData drop = null;
		ArrayList<GameKeepSeatData> newdata = new ArrayList<GameKeepSeatData>();
		GameKeepSeatAllData allData = keep.get(guid);

		synchronized (allData) {
			ArrayList<GameKeepSeatData> data = allData.getDataList();
			for (GameKeepSeatData dd : data) {
				if (dd.isIsofflinekeep() == isofflinekeep && dd.getGameName().equals(gameName)
						&& dd.getHalltype().equals(halltype) && dd.getSeatid().equals(seatid)
						&& dd.getMachineid() == machineid) {
					drop = dd;
					continue;
				}

				newdata.add(dd);
			}

			if (drop == null) {
				errorCode = ErrorCode.SUCCESS;
				return;

			}

			// 讀取modelid
			String modeldbid = "";
			PlayerKeepSeatModel model = allData.getDataModel();

			// 回存資料庫
			JSONArray keeplist = new JSONArray();

			for (GameKeepSeatData dd : newdata) {
				JSONObject obj = dd.toJson();
				keeplist.add(obj);
			}

			JSONObject db = new JSONObject();
			db.put("keep", keeplist);

			model.setContent(db.toJSONString());
			model.setModifytime(TimeSys.currentTimeMills());

			boolean result = PlayerKeepSeatDAO.modifyKeepSeatData(model);
			if (result != true) {
				errorCode = ErrorCode.UPDATE_KEEPDATA_ERR;
				return;
			}

			allData.setDataList(newdata);
			keep.put(guid, allData);

			// 做log
			PlayerKeepSeatLogModel log = new PlayerKeepSeatLogModel();
			log.setStat(PlayerKeepSeatLogModel.REMOVE);
			log.setGamename(drop.getGameName());
			log.setHalltype(drop.getHalltype());
			log.setSeatid(drop.getSeatid());
			log.setMachineid(drop.getMachineid());
			log.setGuid(guid);
			log.setTimeout(drop.getTimeout());
			log.setIsofflinekeep(drop.isIsofflinekeep());
			log.setCreatetime(TimeSys.currentTimeMills());
			result = PlayerKeepSeatLogDAO.addPlayerKeepLog(log);

			errorCode = (result) ? ErrorCode.SUCCESS : ErrorCode.ADD_KEEPDATA_LOG_ERR;
		} // synchronized (allData) {
	}

	@SuppressWarnings("static-access")
	@Override
	public void callBackExecute(Object... userData) throws Exception {
		switch (methodType) {
		case DCLiteMessageType.FISH_DM_CANCELOFFLINEKEEPDATA:
			((ISlotDoormanProcesser)Contexts.getProcesser()).onLobbyCancelOfflineDataBack(errorCode, userData);
			break;
		}
	}
}
